using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using AandEDLL;
using AandEDLL.DataAccess;

namespace ASA
{
    public partial class CategoryDialog : ASA.BaseDialog
    {
        private Category selectedCategory;
        public Category SelectedCategory
        {
            get { return selectedCategory; }
            set { selectedCategory = value; }
        }

        private Boolean selectMode;

        private String mode;

        public CategoryDialog(Boolean selectMode)
        {
            InitializeComponent();
            RefreshList();
            this.selectMode = selectMode;
            InitializeMode();
        }

        private void InitializeMode()
        {
            if (selectMode)
            {
                this.Title = "Select Category";
                this.okButton.Visible = true;
                removeButton.Enabled = false;
                removeButton.Visible = false;
                cancelButton.Top = saveButton.Top;
                saveButton.Top = removeButton.Top;
            }
            else
            {
                this.Title = "Manage Categories";
                this.okButton.Visible = false;
                this.AcceptButton = null;
                removeButton.Enabled = true;
            }
        }

        private void RefreshList()
        {
            categoryListBox.DataSource = Accessor.getCategories();
            categoryListBox.DisplayMember = "Name";
            categoryListBox.ValueMember = "CategoryID";
        }

        private void button3_Click(object sender, EventArgs e)
        {
            prepareToSave("New");
        }

        private void prepareToSave(String mode)
        {
            this.AcceptButton = null;
            this.mode = mode;
            categoryNameTextBox.Enabled = true;
            categoryNameTextBox.Focus();
            saveButton.Enabled = true;
            cancelButton.Enabled = true;
            updateButton.Enabled = false;
            removeButton.Enabled = false;
            newButton.Enabled = false;
            categoryListBox.Enabled = false;
        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            try
            {
                Category category = (Category)categoryListBox.SelectedItem;
                categoryNameTextBox.Text = category.Name;
                categoryNameTextBox.Tag = category;
                prepareToSave("Update");
            }
            catch(Exception ex)
            {
                MessageBox.Show("No item selected to update.");
            }
        }

        private void releaseSave()
        {
            mode = "";
            this.AcceptButton = okButton;
            categoryNameTextBox.Enabled = false;
            saveButton.Enabled = false;
            updateButton.Enabled = true;
            if (!selectMode)
                removeButton.Enabled = true;
            newButton.Enabled = true;
            categoryListBox.Enabled = true;
            cancelButton.Enabled = false;
        }

        private void saveButton_Click(object sender, EventArgs e)
        {
            saveCategory();
        }

        private void saveCategory()
        {
            try
            {
                if (mode == "Update")
                {
                    Category category = (Category)categoryNameTextBox.Tag;
                    category.Name = categoryNameTextBox.Text;
                    Category[] categories = new Category[1];
                    categories[0] = category;
                    Accessor.updateCategory(categories);
                }

                if (mode == "New")
                {
                    Category category = new Category();
                    category.Name = categoryNameTextBox.Text;
                    category.SavedDate = DateTime.Now;
                    Accessor.insertCategory(category);
                }
                RefreshList();
                releaseSave();
                if (categoryNameTextBox.Text.Trim() != "")
                {
                    int index =categoryListBox.FindStringExact(categoryNameTextBox.Text);
                    categoryListBox.SetSelected(index, true);  
                }
                categoryNameTextBox.Clear();
            }
            catch (Exception error)
            {
                MessageBox.Show(this, error.Message, "ASA", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void cancelButton_Click(object sender, EventArgs e)
        {
            categoryNameTextBox.Tag = null;
            releaseSave();
        }

        private void removeButton_Click(object sender, EventArgs e)
        {
            if(categoryListBox.SelectedItem == null)
            {
                MessageBox.Show("No item selected to remove.");
                return;
            }

            if (MessageBox.Show(this, "Are you sure you want to remove this category? \n" +
                "By removing the category, all items tied to this category will be removed.",
                "ASA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                Category[] categories = new Category[1];
                categories[0] = (Category)categoryListBox.SelectedItem;
                Accessor.deleteCategory(categories);
                RefreshList();
            }

            
           
        }

        private void okButton_Click(object sender, EventArgs e)
        {
            if (categoryListBox.SelectedItem == null)
            {
                MessageBox.Show(this, "Please select a category before pressing \"OK\"", "ASA", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            selectedCategory = (Category)categoryListBox.SelectedItem;
            this.DialogResult = DialogResult.OK;
            this.Close();
        }

        private void categoryListBox_DoubleClick(object sender, EventArgs e)
        {
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CategoryDialog_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (unsavedChanges())
            {
                if (MessageBox.Show(this, "There are unsaved changes. \n" +
                "Are you sure you want to close this window?",
                "ASA", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.No)
                {
                    e.Cancel = true;
                }
            }
        }

        private Boolean unsavedChanges()
        {
            if (saveButton.Enabled == true)
                return true;
            else
                return false;
        }

        private void CategoryDialog_KeyDown(object sender, KeyEventArgs e)
        {
            
        }

        private void categoryNameTextBox_KeyDown(object sender, KeyEventArgs e)
        {
            if (saveButton.Enabled)
            {
                if (e.KeyCode == Keys.Enter)
                {
                    saveCategory();
                }
            }
        }
    }
}

